<?php
session_start();

// 检查用户是否已经登录
if (!isset($_SESSION['logged_in']) || !$_SESSION['logged_in']) {
    // 如果用户未登录，跳转到登录页面
    header('Location: login.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>文章编辑</title>
    <!-- 引入 jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <!-- 引入 Summernote 的 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-lite.min.css" rel="stylesheet">
    <!-- 引入 Summernote 的 JS -->
    <script src="https://cdn.jsdelivr.net/npm/summernote@0.8.20/dist/summernote-lite.min.js"></script>
    <!-- 引入 Summernote 的中文语言包 -->
    <script src="https://cdn.bootcss.com/summernote/0.8.20/lang/summernote-zh-CN.js"></script>
</head>
<body style="background-color: white; padding: 20px; font-family: Arial, sans-serif;">
    <form action="submit.php" method="post">
        <label for="title">تېما：</label>
        <input type="text" id="title" name="title" required style="border: 2px solid #007BFF; border-radius: 8px; padding: 8px; width: 100%; max-width: 600px; margin-bottom: 15px;"><br><br>
        
        <textarea id="editor" name="content" style="width: 100%; max-width: 600px; height: 300px;"></textarea><br><br>
        <script>
            $(document).ready(function() {
                $('#editor').summernote({
                    placeholder: 'مەزمۇننى مىشەگە يازسا بالو...',
                    tabsize: 2,
                    height: 300,
                    lang: 'zh-CN', // 设置语言为中文
                    toolbar: [
                        ['style', ['style']],
                        ['font', ['bold', 'underline', 'clear']],
                        ['color', ['color']],
                        ['para', ['ul', 'ol', 'paragraph']],
                        ['table', ['table']],
                        ['insert', ['link', 'picture', 'video']],
                        ['view', ['fullscreen', 'codeview', 'help']]
                    ],
                    callbacks: {
                        onImageUpload: function(files, editor, welEditable) {
                            sendFile(files[0], editor, welEditable);
                        }
                    }
                });
            });

            function sendFile(file, editor, welEditable) {
                var data = new FormData();
                data.append("file", file);
                $.ajax({
                    data: data,
                    type: "POST",
                    url: "upload.php",
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function(response) {
                        console.log(response); // 调试信息
                        var result = JSON.parse(response);
                        if (result.uploaded === 1) {
                            // 插入图片到编辑器
                            var image = $('<img>').attr('src', result.url);
                            $('#editor').summernote('insertNode', image[0]);
                        } else {
                            alert('图片上传失败: ' + result.error);
                        }
                    },
                    error: function() {
                        alert('图片上传失败');
                    }
                });
            }
        </script>
        <label for="editor" style="font-size: 16px;">رەسىم چىققاندىن كىيىن چەكسىڭىز كىچىكلەيدىغان يەر چىقىدۇ</label>
        </br>
        <a href="index.php" class="button" style="background-color: #5cb85c; color: white; padding: 10px 20px; border: none; border-radius: 5px; text-decoration: none; margin-right: 10px;">باشبەت</a>
        <input type="submit" class="button" value="يوللاش发送" style="background-color: #5cb85c; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer;">
    </form>
</body>
</html>